/*
 * Created by JFormDesigner on Tue May 11 10:36:45 CST 2021
 */

package love.yuanmengrobot.wuziqi.UIyc;


import love.yuanmengrobot.wuziqi.Util.User;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.sql.*;
import java.util.ArrayList;
//import java.util.Objects;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import static java.lang.Thread.sleep;

/**
 * @author 1
 */
public class ScoreWin extends JFrame {
    boolean ack=false;

    public boolean isAck() {
        return ack;
    }

    public void setAck(boolean ack) {
        this.ack = ack;
    }

    public ScoreWin(Object[][] objects) {

        this.data = objects;
        initComponents();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        panelWin = new JPanel();
        scrollPane1 = new JScrollPane();
        tableResult = new JTable();
        button1QueDing = new JButton();
        label1 = new JLabel();

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== panelWin ========
        {
            panelWin.setLayout(null);

            //======== scrollPane1 ========
            {
                scrollPane1.setViewportView(tableResult);
            }
            panelWin.add(scrollPane1);
            scrollPane1.setBounds(40, 50, 385, 240);

            //---- button1QueDing ----
            button1QueDing.setText("\u786e\u5b9a");
            panelWin.add(button1QueDing);
            button1QueDing.setBounds(180, 290, 100, 35);

            //---- label1 ----
            label1.setText("\u6e38\u620f\u7ed3\u675f\uff01");
            label1.setFont(label1.getFont().deriveFont(label1.getFont().getSize() + 8f));
            panelWin.add(label1);
            label1.setBounds(new Rectangle(new Point(185, 15), label1.getPreferredSize()));
        }
        contentPane.add(panelWin);
        panelWin.setBounds(0, 0, 445, 350);

        contentPane.setPreferredSize(new Dimension(455, 385));
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents

        DefaultTableModel tableModel = new DefaultTableModel(allData(), head) {//如何往表里加数据
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        tableResult.setModel(tableModel);

        button1QueDing.addActionListener(new AbstractAction() {//给按钮添加监听事件
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                System.out.println("确定被点击了");
                ack=true;
                setVisible(false);
//                try {
//                    System.out.println("休息···");
//                    sleep(10000);
//
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
                dispose();//关闭窗体

//                System.out.println(data[0][0]);
//                System.out.println(data[0][1]);
//                updataUserScore(data);
            }
        });

        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//全部关闭
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel panelWin;
    private JScrollPane scrollPane1;
    private JTable tableResult;
    private JButton button1QueDing;
    private JLabel label1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
    private Object[][] data = null;
    private String head[] = {"昵称", "收获积分"};

    public Object[][] allData() {

//        data = new Object[1][2];
//
//        data[0][0] ="15022222222";
//        data[0][1] ="60";

        return data;
    }

    public void updataUserScore(String id,String score) {

        Connection conn = null;//声明连接
        String url = "jdbc:oracle:thin:@112.74.190.130:1521:orcl";//地址
        Statement stmt = null;//陈述语句
        ResultSet rs = null;//查询结构集合

//        System.out.println(data[0][0] + "$");//CHJY
//        String id = data[0][0].toString();
//        System.out.println(data[0][1] + "$");//60
//        String score = data[0][1].toString();

        String sql = " UPDATE wuziqiuser SET userscore=(select userscore from wuziqiuser where USERID=" + id + ")+" + score + " where USERID=" + id;//里面无需分号
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            conn = DriverManager.getConnection(url, "scott", "scott1316");//登录数据库的账号密码
            stmt = conn.createStatement();
            int res = stmt.executeUpdate(sql);
            System.out.println("更新结果：" + res);
            sql = "INSERT INTO wuziqiscore VALUES(" + id + "," + score + ",0,sysdate)";
            stmt.executeUpdate(sql);
            System.out.println("更新结果：" + res);

//        INSERT INTO wuziqiscore VALUES(15022222222,60,0,sysdate);--字符只需单引号括起

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
//        try {
////            rs.close();
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }


    }


//    public static void main(String[] args) {
//ScoreWin scoreWin=new ScoreWin();
//
//
//    }


}
